<template>
    <div class="app-container">
        <div style="text-align: center">更新维保任务</div>
        <div style="text-align: center;margin-bottom: 30px">
        </div>
        <div style="width: 50%;margin:0px auto;vertical-align:middle;height:100%">
            <el-form :model="maintenanceTaskForm" ref="maintenanceTaskForm" :rules="maintenanceTaskFormRules" label-width="100px">
                <el-form-item label="维保企业" prop="maintenaceEnterpriseId" >
                    <el-select v-model="maintenanceTaskForm.maintenaceEnterpriseId" placeholder="请选择企业" style="width: 100%" @change="getInUserList" v-if="enterPriseHidden">
                        <el-option v-for="(item,index) in maintenanceEnterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="维保人员" prop="userId" >
                    <el-select v-model="maintenanceTaskForm.userId" placeholder="维保人员" style="width: 100%">
                        <el-option v-for="(item,index) in getInUserList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="业主企业" prop="enterpriseId" >
                    <el-select v-model="maintenanceTaskForm.enterpriseId" placeholder="请选择企业" style="width: 100%">
                        <el-option v-for="(item,index) in enterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="任务状态" prop="status">
                    <el-select v-model="maintenanceTaskForm.status" placeholder="请选择设备状态" style="width: 100%">
                        <el-option :value="0" label="已下发待分配"></el-option>
                        <el-option :value="1" label="已分配待执行"></el-option>
                        <el-option :value="2" label="开始执行"></el-option>
                        <el-option :value="3" label="执行中"></el-option>
                        <el-option :value="4" label="执行完成"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始执行时间" prop="startExecuteTime">
                    <el-date-picker
                            v-model="maintenanceTaskForm.startExecuteTime"
                            type="datetime"
                            placeholder="选择日期时间"
                            style="width: 100%">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker
                            v-model="maintenanceTaskForm.endTime"
                            type="datetime"
                            placeholder="选择日期时间"
                            style="width: 100%">
                    </el-date-picker>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelMaintenanceTask">取消</el-button>

            <el-button :loading="addLoading"  type="primary" @click="updateMaintenanceTask">更新</el-button>
        </div>
    </div>
</template>

<script>

import {getInMaintenanceInfoVo, updateInMaintenanceInfo} from "@/api/maintenanceRecord";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getInUserList} from "@/api/enterprise";
export default {
    name: "edit",
    data(){
        return{
            maintenanceTaskForm:{

            },
            maintenanceTaskFormRules:{
                enterpriseId: [{ required: true, message: '请选择企业', trigger: 'blur' }],
                manitenanceEnterpriseId: [{ required: true, message: '请选择维保企业', trigger: 'blur' }],
            },
            addLoading:false,
            equipmentList:[],
            enterpriseList:[],
            maintenanceEnterpriseList:[],
            userList:[]

        }
    },
    computed: {
      enterPriseHidden(){
        let roleCode = this.$store.getters.roleCode;
        return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"||roleCode=="WB-ADMIN"
      }
    },
    created() {
            this.getInMaintenanceInfoVo()
        this.getInEnterpriseDominantList();

        this.getInOwnerDominantList();
    },
    methods:{
        cancelMaintenanceTask(){
          window.history.back()
        },
        getInMaintenanceInfoVo() {
            getInMaintenanceInfoVo({ucId: this.$route.params.ucId}).then(res =>{
                console.log(res)
                this.maintenanceTaskForm = res.data
                this.getInUserList(this.maintenanceTaskForm.maintenaceEnterpriseId);
            }).catch(res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },
        updateMaintenanceTask() {
            this.addLoading = true
            this.$refs['maintenanceTaskForm'].validate(valid => {
                if (valid) {
                    updateInMaintenanceInfo(this.maintenanceTaskForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新维保任务成功"
                        })
                        this.cancelMaintenanceTask()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList() {
            getInEnterpriseDominantList({dominantType:9,deleted:false}).then(res => {
                this.maintenanceEnterpriseList=res.data;
            }).catch(res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },
        getInOwnerDominantList() {
            getInEnterpriseDominantList({dominantType: 10,deleted : false}).then(res => {
                this.enterpriseList = res.data;
            }).catch (res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },
        getInUserList(val) {
            getInUserList({flagId:val,deleted:false}).then(res => {
                this.userList = res.data
            }).catch(res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },

    }

}
</script>

<style
        scoped>

</style>